
import { getAllCategory } from '@/api/category'
import { topCategory } from '@/api/constants'
// 分类模块
export default {
  namespaced: true,
  state () {
    return {
      // 分类信息集合
      abc: true,
      list: topCategory.map(item => ({ name: item }))

    }
  },
  mutations: {
    // 获取分类列表
    getcategory (state, list) {
      state.list = list
      // 数据改造,给列表里的每个属性添加一个open属性
      list.forEach(v => {
        v.open = false
      })
      state.abc = false
    },
    // 调用此函数显示弹出层
    show (state, item) {
      const a = state.list.find(v => v.id === item.id)
      a.open = true
    },
    // 调用此函数关闭弹出层
    hide (state, item) {
      const a = state.list.find(v => v.id === item.id)
      a.open = false
    }
  },
  actions: {
    // 异步请求获取数据
    async setcategory ({ state, commit }) {
      const { result } = await getAllCategory()
      // 赋值给state的列表
      commit('getcategory', result)
    }
  }
}
